﻿Public Class clsInhKlient
    Inherits clsKlient

#Region "Attributes"

    Private mvarAdresWysylki As Collection(Of clsAdresWysylki)
    Private mvarAdresWysylkiDelete As Collection(Of clsAdresWysylki)
    Private mvarKlient As Collection(Of clsKlient)

#End Region

#Region "Properties"

    Public Property AdresWysylki() As Collection(Of clsAdresWysylki)
        Get
            Return mvarAdresWysylki
        End Get
        Set(ByVal value As Collection(Of clsAdresWysylki))
            mvarAdresWysylki = value
        End Set
    End Property

    Public Property AdresWysylkiDelete() As Collection(Of clsAdresWysylki)
        Get
            Return mvarAdresWysylkiDelete
        End Get
        Set(ByVal value As Collection(Of clsAdresWysylki))
            mvarAdresWysylkiDelete = value
        End Set
    End Property

    Public Property Klient() As Collection(Of clsKlient)
        Get
            Return mvarKlient
        End Get
        Set(ByVal value As Collection(Of clsKlient))
            mvarKlient = value
        End Set
    End Property

#End Region

#Region "Methods"

    Public Sub New()
        MyBase.New()
        mvarAdresWysylki = New Collection(Of clsAdresWysylki)
        mvarAdresWysylkiDelete = New Collection(Of clsAdresWysylki)
        mvarKlient = New Collection(Of clsKlient)
    End Sub

    Public Sub New(ByVal ID As Integer, ByVal lvarDbHolder As clsDataBase)

        MyBase.New(ID, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarAdresWysylki = New Collection(Of clsAdresWysylki)
        mvarAdresWysylkiDelete = New Collection(Of clsAdresWysylki)

        Dim lvarTabel As DataTable = clsKlient.PobierzWszystkieAdresy(lvarDbHolder, VAR_ERROR_HANDLER, ID)

        If Not lvarTabel Is Nothing Then
            For Each lvarRow As DataRow In lvarTabel.Rows
                mvarAdresWysylki.Add(New clsAdresWysylki(CInt(lvarRow.Item("id")), lvarDbHolder, VAR_ERROR_HANDLER))
            Next
        End If

    End Sub

    Public Overloads Sub Save(ByVal lvarDbHolder As clsDataBase)

        lvarDbHolder.BeginTrans()

        MyBase.Save(lvarDbHolder, VAR_ERROR_HANDLER)

        For Each lvarAdresWysylkiDelete As clsAdresWysylki In mvarAdresWysylkiDelete
            lvarAdresWysylkiDelete.Delete(lvarDbHolder, VAR_ERROR_HANDLER)
        Next

        For Each lvarAdresWysylki As clsAdresWysylki In mvarAdresWysylki
            lvarAdresWysylki.KlientId = CStr(MyBase.Id)
            lvarAdresWysylki.Save(lvarDbHolder, VAR_ERROR_HANDLER)
        Next

        lvarDbHolder.CommitTrans()

    End Sub

    Public Sub SzukajKlientow(ByVal lvarDbHolder As clsDataBase, ByRef ErrorHandler As clsErrorHandler, ByVal Zapytanie As String)

        Dim lvarReader As OdbcDataReader = Nothing
        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_oem", "cls_klient", 1, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_zapytanie", Zapytanie)
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                While lvarReader.Read
                    mvarKlient.Add(New clsKlient(CInt(lvarReader.Item("id")), lvarDbHolder, VAR_ERROR_HANDLER))
                End While
            End If

        End If

    End Sub

#End Region

End Class
